package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/24.
 */

public class AddBankActivity extends AppCompatActivity {

    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.real_name_tv)
    TextView realNameTv;
    @BindView(R.id.realname_edit)
    EditText realnameEdit;
    @BindView(R.id.sex_tv)
    TextView sexTv;
    @BindView(R.id.type_iv)
    ImageView typeIv;
    @BindView(R.id.type_tv)
    TextView typeTv;
    @BindView(R.id.type_bt)
    RelativeLayout typeBt;
    @BindView(R.id.bankcard_num)
    TextView bankcardNum;
    @BindView(R.id.idcard_edit)
    EditText idcardEdit;
    @BindView(R.id.phone_num)
    TextView phoneNum;
    @BindView(R.id.phone_edit)
    EditText phoneEdit;
    @BindView(R.id.succss_bt)
    Button succssBt;
    private Dialog bankdialog;
    @BindView(value = R.id.bankcardname_edit)
    EditText bankcardname_edit;
    public Dialog loadingdialog = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addbank);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
    }

    private void initComponent() {
        loadingdialog = new Dialog(this);
        loadingdialog.setContentView(R.layout.dialog_loading);
        loadingdialog.setCanceledOnTouchOutside(false);
        loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        bankdialog = new Dialog(this);
        bankdialog.setContentView(R.layout.dialog_banktype);
        bankdialog.setCanceledOnTouchOutside(false);
        bankdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View chuxu = bankdialog.findViewById(R.id.chuxu);
        chuxu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                typeTv.setText("储蓄卡");
                bankdialog.dismiss();
            }
        });
        View xinyong = bankdialog.findViewById(R.id.xinyong);
        xinyong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                typeTv.setText("信用卡");
                bankdialog.dismiss();
            }
        });
        View exit = bankdialog.findViewById(R.id.exit);
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bankdialog.dismiss();
            }
        });
    }

    @OnClick(value = {R.id.backbt, R.id.succss_bt, R.id.type_bt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.succss_bt:
                if(TextUtils.isEmpty(realnameEdit.getText().toString().trim())){
                    Toast.makeText(getApplicationContext(),"请填写真实姓名",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(bankcardname_edit.getText().toString().trim())){
                    Toast.makeText(getApplicationContext(),"请填写银行名称",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!bankcardname_edit.getText().toString().trim().contains("银行")){
                    Toast.makeText(getApplicationContext(),"请正确填写银行名称",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(idcardEdit.getText().toString().trim())){
                    Toast.makeText(getApplicationContext(),"请填写银行卡号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(idcardEdit.getText().toString().trim().length()<16){
                    Toast.makeText(getApplicationContext(),"请确认是否是银行卡",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(phoneEdit.getText().toString().trim())){
                    Toast.makeText(getApplicationContext(),"请填写银行预留号码",Toast.LENGTH_SHORT).show();
                    return;
                }
                addBank(realnameEdit.getText().toString().trim(),
                        typeTv.getText().toString().trim(),
                        bankcardname_edit.getText().toString().trim(),
                        idcardEdit.getText().toString().trim(),
                        phoneEdit.getText().toString().trim()
                );
                break;
            case R.id.type_bt:
                bankdialog.show();
                break;
        }
    }
    public void addBank(String userbankcardRealname,String userbankcardType,
                        String userbankcardBankname,String userbankcardBankcard,
                        String userbankcardIphone){
        final List<User> userList= Xutils.queryAll(getApplication(),User.class);
        if(userList==null||userList.size()==0){
            return;
        }
        User user=userList.get(0);
        loadingdialog.show();
        Map<String,String> map=new HashMap<>();
        map.put("userId",user.getUserId()+"");
        map.put("userbankcardRealname",userbankcardRealname);
        map.put("userbankcardBankname",userbankcardBankname);
        map.put("userbankcardBankcard",userbankcardBankcard);
        map.put("userbankcardType",userbankcardType);
        map.put("userbankcardIphone",userbankcardIphone);
        Xutils.POST(HttpUrl.adduserbank, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                loadingdialog.dismiss();

                try {
                    JSONObject jsonObject=new JSONObject(result);
                    Toast.makeText(getApplicationContext(),jsonObject.getString("message"),Toast.LENGTH_SHORT).show();
                    switch (jsonObject.getInt("state")){
                        case HttpCode.SUCCESS:
                            finish();
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                loadingdialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
